package cn.tedu.v3;

import java.io.*;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.Buffer;
import java.util.Scanner;

/**
 * 多人聊天室 Version 3
 * Server服务器：找到CS多线程类
 * 1.读入放入线程中
 * 2.写出直接写在run方法
 */
public class Server {
    private ServerSocket serverSocket;

    public Server() {
        try {
            serverSocket = new ServerSocket(9686);
        } catch (BindException e) {
            System.out.println("端口号已经被占用,请检查端口号");
        } catch (IOException e) {
            System.out.println("客户端主动退出了");
        }
    }

    public void start() {
        BufferedReader br = null;
        InputStream is = null;
        InputStreamReader isr = null;
               Socket socket=null;   
        String ip=null;
        System.out.println("等待客户端连接");
        while (true) {
            try {

                socket = serverSocket.accept();

                new Thread(new CS(socket)).start();

            } catch (IOException e) {
                System.out.println(ip+"客户端链接失效了~");
            } finally {

               if(serverSocket!=null&&serverSocket.isClosed()){
                   try {
                       serverSocket.close();
                   } catch (IOException e) {
                       System.out.println("服务器关闭失败~~~~~");
                   }
                   System.out.println("服务器关闭成功~~~~~Over");
               }
            }
        }

    }

    public static void main(String[] args){
        Server server = new Server();
        server.start();
    }

    private class CS implements Runnable {

        private Socket socket;

        private  String ip;

        public CS(Socket s){
            this.socket=s;
            ip=socket.getInetAddress().getHostAddress();
        }

        public void run() {
            //输入流--读入功能：存入线程中
            PrintWriter pw =null;
            
          try {
              pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
              BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));

              new Thread(() -> {
                  try {
                      while (true) {
                          String str = br.readLine();//获取一行字符串，交给str保存
                          System.out.println(ip + "客户端端说：" + str);//打印  客户端发过来文本
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }).start();

              //客户端一直跟服务器端----说【循环】
              System.out.println("服务器端用户：请输入文字：");
              Scanner input = new Scanner(System.in);//测试---控制台输入文本
              while (true) {//死循环---一直与服务器说话
                  String say = input.nextLine();
                  pw.println("服务器端说：" + say);        //写出  一行行文字
                  System.out.println("写出完成嘞~");

              }

                  } catch (UnsupportedEncodingException e) {
                      System.out.println("请检查客户端/服务器端编码集：不一致");
                  } catch (SocketException e) {
                      System.out.println("客户端链接被重置");
                  } catch (IOException e) {
                      System.out.println("客户端链接失效了~");
                  } finally {
                              if(pw!=null)
                                pw.close();
                               if (socket != null && !socket.isClosed()) {
                                   System.out.println(ip + "服务器端关闭成功！！！success！");
                                   try {
                                       socket.close();
                                   } catch (IOException e) {
                              System.out.println(ip + "一个客户端断开链接了~~~");
                          }
                          System.out.println(ip + "一个客户端退出服务器链接~~");
                      }


                  }
              }
          }

                }


